package cn.kgc.controllers;


import cn.kgc.pojo.District;
import cn.kgc.service.AirService;
import cn.kgc.service.DistrictService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import cn.kgc.pojo.Air;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("test")
public class AirMain {
    @Autowired
    AirService airService;
    @Autowired
    DistrictService districtService;

    @RequestMapping("findAll")
    public String findAll(Model model,
                          @RequestParam(value="pageIndex", required = false, defaultValue="1")Integer pageIndex,
                          @RequestParam(value="district", required = false, defaultValue="0") Integer district){
        PageHelper.startPage(pageIndex, 3);
        List<Air> airs = airService.airList(district);
        PageInfo<Air> pageInfo = new PageInfo<>(airs);

        List<District> directList = districtService.district();

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("directList", directList);
        model.addAttribute("district", district);

        return "index3";
    }

    @RequestMapping("findType")
    public String findType(Model model){
        List<District> directList = districtService.district();
        model.addAttribute("directList", directList);
        return "insertAir";
    }

    @RequestMapping("insert")
    public String insert(Model model, @RequestParam("type")String district,
                         @RequestParam("date") String date,
                         @RequestParam("PM1")String PM10,
                         @RequestParam("PM2")String PM25,
                         @RequestParam("station")String station){

        Air air = new Air();
        air.setDistrictId(Integer.parseInt(district));
        air.setMonitoringStation(station);
        air.setPm10(Integer.parseInt(PM10));
        air.setPm25(Integer.parseInt(PM25));
        /*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(date); */      //将Date类型转换成String类型
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date time = null;
        try {
            time = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        air.setMonitorTime(time);
        air.setLastModifyTime(new Date());
        int result = airService.insert(air);
        if(result > 0){
            System.out.println("添加成功");
            model.addAttribute("msg", "添加成功");
            return "redirect:/test/findAll";
        }else{
            System.out.println("添加失败");
            model.addAttribute("msg", "添加失败");
            return "redirect:/test/findAll";
        }
    }

    @RequestMapping("findId")
    public String findId(@RequestParam("id")Integer id, Model model){
        Air air = airService.airGetById(id);
        model.addAttribute("air", air);
        List<District> directList = districtService.district();
        model.addAttribute("directList", directList);
        return "show";
    }

}
